package com.fbt.andon.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fbt.andon.domain.Line;
import com.fbt.andon.domain.Plant;
import com.fbt.andon.domain.StopLine;
import com.fbt.andon.domain.Task;
import com.fbt.andon.service.impl.LineServiceImpl;
import com.fbt.andon.service.impl.PlantServiceImpl;
import com.fbt.andon.service.impl.StopLineServiceImpl;
import com.fbt.andon.service.impl.TaskServiceImpl;
import com.linkwechat.common.core.controller.BaseController;
import com.linkwechat.common.core.domain.AjaxResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 工厂表 前端控制器
 * </p>
 *
 * @author 冯涛
 * @since 2021-09-28
 */
@RestController
@RequestMapping("/plant")
public class PlantController extends BaseController {

    @Autowired
    private PlantServiceImpl plantService;
    @Autowired
    private LineServiceImpl lineService;
    @Autowired
    private StopLineServiceImpl stopLineService;
    @Autowired
    private TaskServiceImpl taskService;

    /**
     * 查询该表所有信息
     */
    @GetMapping("/getline")
    @ApiOperation("查询工厂所有信息")
    public AjaxResult findlineList() {
        startPage();
        return AjaxResult.success("查询成功", plantService.list(null));
    }


    /**
     * 获取停线详细信息
     */
    //@PreAuthorize(hasPermi = "andon:line:query")
    @GetMapping("/selectByPlaneNumber/{id}")
    @ApiOperation("根据工厂id获取产线信息，参数工厂id")
    public AjaxResult getDetails(@PathVariable("id") String plantNumber) {
        //工厂可以查到工厂所有的小组信息
        QueryWrapper<Line> wrapper = new QueryWrapper<>();
        wrapper.like("plant_number", plantNumber);
        //可以查到停线的信息
        return AjaxResult.success("获取成功", lineService.list(wrapper));
    }

    /**
     * 获取停线详细信息
     */
//    //@PreAuthorize(hasPermi = "andon:line:query")
    @GetMapping("/selectDetails/{id}")
    @ApiOperation("根据工厂号获取详情")
    public AjaxResult getInfo(@PathVariable("id") String planeNumber) {
        //这里该弄hashMap
        //工厂可以查到工厂所有的小组信息
        QueryWrapper<StopLine> wrapper = new QueryWrapper<>();
        wrapper.eq("plant_id", planeNumber);

        QueryWrapper<Task> wrapper1 = new QueryWrapper<>();
        wrapper1.orderByDesc("create_time").last("limit 1");

        QueryWrapper<Line> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("plant_number", planeNumber);
        //工厂可以查到工厂所有的小组信息
        List<Integer> list = new ArrayList<Integer>();
        /*
         * 1，产线数量
         * 2，近一周停线数
         * 5，今日生产情况
         * */
        list.add(lineService.count(wrapper2));
        list.add(stopLineService.count(wrapper));
        Task one = taskService.getOne(wrapper1);
//        计划数
        list.add(one.getPlan());
//        完成数
        list.add(one.getOk());
        //可以查到停线的信息
        return AjaxResult.success("获取成功", list);
    }

    /**
     * 新增停线
     */
//    //@PreAuthorize(hasPermi = "andon:line:add")
    @PostMapping("/add")
    @ApiOperation("新增产线，参数Plant")
    public AjaxResult add(@RequestBody Plant plant) {
        return AjaxResult.success("新增成功", plantService.save(plant));
    }

    /**
     * 修改停线
     */
//    //@PreAuthorize(hasPermi = "andon:line:edit")
    @PostMapping("/update")
    @ApiOperation("根据id修改工厂，参数stopLine")
    public AjaxResult edit(@RequestBody Plant plant) {
        return AjaxResult.success("修改成功", plantService.updateById(plant));
    }

    /**
     * 删除停线
     */
//    //@PreAuthorize(hasPermi = "andon:line:remove")
    @PostMapping("/delete/{ids}")
    @ApiOperation("根据id批量删除产线，参数id")
    public AjaxResult remove(@PathVariable("ids") Long[] ids) {
        return AjaxResult.success("删除成功", plantService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 生成停线excel
     */
//    //@PreAuthorize(hasPermi = "andon:line:remove")
    @PostMapping("/GenerateExcel/{number}")
    @ApiOperation("根据最新时间生成停线excel，参:条数")
    public AjaxResult GenerateExcel(@PathVariable("number") int number) {
        QueryWrapper<StopLine> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time").last("limit " + number);
        return AjaxResult.success("生成excel成功", stopLineService.list(wrapper));
    }
}



